<?php
/**
 * Service of Admin
 *
 * @author fsqijy
 */

require_once BaseUtil::getModulePath("base")  .  '/services/BaseService.php';
require_once BaseUtil::getModulePath("acl")  .  '/models/AdminDao.php';
require_once BaseUtil::getModulePath("acl")  .  '/models/RolesDao.php';
require_once BaseUtil::getModulePath("acl")  .  '/models/AdminDepartmentDao.php';
require_once BaseUtil::getModulePath("acl")  .  '/models/RolePermissionsDao.php';
require_once BaseUtil::getModulePath("acl")  .  '/models/AdminPermissionDao.php';
require_once BaseUtil::getModulePath("department")  .  '/models/DepartmentDao.php';


class AdminService extends BaseService {

    private $departmentDao;

    private $roleDao;

    private $adminDao;

    private $adminDepartmentDao;

    private $rolePermissionDao;

    private $adminPermissionDao;
    
    public function __construct ()
    {
        parent::__construct();
        $this->departmentDao = new DepartmentDao();
        $this->departmentDao->setDb($this->db);

        $this->roleDao = new RolesDao();
        $this->roleDao->setDb($this->db);

        $this->adminDao = new AdminDao();
        $this->adminDao->setDb($this->db);

        $this->adminDepartmentDao = new AdminDepartmentDao();
        $this->adminDepartmentDao->setDb($this->db);

        $this->rolePermissionDao = new RolePermissionsDao();
        $this->rolePermissionDao->setDb($this->db);

        $this->adminPermissionDao = new AdminPermissionDao();
        $this->adminPermissionDao->setDb($this->db);
    }

    /**
     * get all departments
     */
    public function getDepartments(){
        $departmentData =  $this->departmentDao->getDepartments();
        $departments = array();
        foreach($departmentData as $department){
            $departments[$department['id']] = $department['name'];
        }
        return array($departmentData,$departments);
    }

    /**
     * get all roles
     */
    public function getRoles(){
        $roleData = $this->roleDao->getRoles();
        $roles = array();
        foreach($roleData as $role){
            $roles[$role['id']] = $role['role_name'];
        }
        return $roles;
    }

    public function addAdmin($admin,$role,$departments){
        //Insert admin
        $adminId = $this->adminDao->insertData($admin);
        //Insert Admin Departments
        if(!empty($departments)){
            foreach($departments as $departmentId){
                $adminDepartment = array();
                $adminDepartment['admin_id'] = $adminId;
                $adminDepartment['department_id'] = $departmentId;
                $this->adminDepartmentDao->insertData($adminDepartment);
            }
        }
        //Insert permission
        $permissions = $this->rolePermissionDao->getPermissionsByRoleId($role);
        if(!empty($permissions)){
            foreach($permissions as $permission){
                $adminPermission = array();
                $adminPermission['admin_id'] = $adminId;
                $adminPermission['permission_key'] = $permission;
                $this->adminPermissionDao->insertData($adminPermission);
            }
        }
    }

    public function deleteAdmins($ids){
        foreach($ids as $id){
            //delete admin_departments
            $adminDepartment = array("is_delete" => 1);
            $this->adminDepartmentDao->updateAdminDepartment($adminDepartment, $id);
            //delete admin_permissions
            $adminPermission = array("is_delete" => 1);
            $this->adminPermissionDao->updateAdminPermission($adminPermission,$id);
            //delete admin
            $admin = array("is_delete" => 1);
            $this->adminDao->updateAdmin($admin, $id);
        }
    }

    public function getAdminById($id){
       return $this->adminDao->getAdminById($id);
    }

    public function getDepartmentsByAdmin($adminId){
        $departmentData = $this->adminDepartmentDao->getDepartmentsByAdmin($adminId);
        $departments = array();
        foreach($departmentData as $department){
            $departments[] = $department['department_id'];
        }
        return $departments;
    }

    public function getPermissionsByAdmin($adminId){
        $permissionData = $this->adminPermissionDao->getPermissionsByAdmin($adminId);
        $permissions = array();
        foreach($permissionData as $permission){
            $permissions[] = $permission['permission_key'];
        }
        return $permissions;
    }

    public function updateAdmin($adminId,$admin,$departments,$permissions){
        $this->adminDao->updateAdmin($admin, $adminId);
        //delete and insert new admin departments
        $this->adminDepartmentDao->deleteAdminDepartment($adminId);
        foreach($departments as $department){
            $adminDepartment = array("admin_id" => $adminId, "department_id" => $department );
            $this->adminDepartmentDao->insertData($adminDepartment);
        }
        //delete and insert new admin permissions
        $this->adminPermissionDao->deleteAdminPermission($adminId);
        foreach($permissions as $permission){
            $adminPermission = array("admin_id" => $adminId, "permission_key" => $permission);
            $this->adminPermissionDao->insertData($adminPermission);
        }
        
    }

    public function updateAccount($adminId,$admin){
        $this->adminDao->updateAdmin($admin, $adminId);
    }

    public function login($email,$password){
        $result = array();
        $admin = $this->adminDao->getAdmin($email, $password);
        if(!empty($admin) && $admin['status'] == 1){
            $result['uid'] = $admin['id'];
            $result['uname'] = $admin['username'];
        }
        return $result;
    }
    
    public function delete($ids) {
    	$adminDepartment = array("is_delete" => 1);
        $this->adminDepartmentDao->updateAdminDepartment($adminDepartment, $ids);    	
    	$adminPermission = array("is_delete" => 1);
        $this->adminPermissionDao->updateAdminPermission($adminPermission,$ids);    	
    	$admin=array("is_delete" => 1);
    	$this->adminDao->updateAdmin($admin,$ids);
    }
    
    public function getAllAdmins(){
    	return $this->adminDao->getAllAdmins();
    }
}